/**
 * Created by jaliu on 2015/2/28.
 */
var lotteryService = require('../../services/backoffice/backOffice-lotteryService');
var messageUtil = require('../../common/utils/messageUtil');
var toolUtil = require('../../common/utils/toolUtil');

exports.addWeekLottery = function (req, res) {
    var input = req.body;

    lotteryService.addWeekLotteryService(input, function (err, result) {
        if (err) {
            logger.error('Exception happened when addWeekLottery ...!' + err.message);
            res.json(messageUtil.composeFailedMessage(req, 'Exception happened when addWeekLottery ...!' + err.message));
        } else {
            res.json(messageUtil.composeSucceededMessage(req, null, null));
        }
    })
}


exports.validateUserForLottery = function (req, res) {
    var token = req.params.token;
    var lotteryId = req.params.lotteryId;
    var clickSource = req.cparams.clickSource;

    lotteryService.validateUserForLotteryService(token, lotteryId, function (err, result) {
        if (err) {
            logger.error('Exception happened when validateUserForLottery ...!' + err.message);
            res.json(messageUtil.composeFailedMessage(req, 'Exception happened when validateUserForLottery ...!' + err.message));
        } else {

            var data = {
                valid: result[0],
                joinedUser: result[2].joinedUser,
                ticketValue: result[2].ticketValue,
                userName: result[2].userName,
                address: result[2].address,
                roomUnit: result[2].roomUnit,
                roomNumber: result[2].roomNumber,
                phone: result[2].phone,
                lotteryTime: result[2].lotteryTime
            };
            res.json(messageUtil.composeSucceededMessage(req, null, data));

            if (clickSource != null && clickSource != 'undefined') {
                var message = {
                    userId: token,
                    contentId: lotteryId,
                    type: 4,
                    clickSource: clickSource,
                    typeDesc: '抽奖',
                    createTime: new Date().getTime()
                }
                toolUtil.putMessageLogMQ(message);
            }
        }
    })
};

exports.generateLotteryTicketForUser = function (req, res) {
    var token = req.params.token;
    var lotteryId = req.params.lotteryId;
    var input = req.body;

    lotteryService.generateLotteryTicketForUserService(token, lotteryId, input, function (err, result) {
        if (err) {
            logger.error('Exception happened when getUserInfo ...!' + err.message);
            res.json(messageUtil.composeFailedMessage(req, err.message));
        } else {
            res.json(messageUtil.composeSucceededMessage(req, null, result));
        }
    })
};

exports.getLuckyUserList = function (req, res) {

    var lotteryId = req.params.lotteryId;

    lotteryService.getLuckyUserListService(lotteryId, function (err, result) {
        if (err) {
            logger.error('Exception happened when getLuckyUserList ...!' + err.message);
            res.json(messageUtil.composeFailedMessage(req, err.message));
        } else {
            res.json(messageUtil.composeSucceededMessage(req, null, result));
        }
    })
};

exports.getLcLuckyUserList = function (req, res) {
    var lotteryId = req.params.lotteryId;
    var week = req.params.week;

    lotteryService.getLcLuckyUserListService(lotteryId, week, function (err, result) {
        if (err) {
            logger.error('Exception happened when getLcLuckyUserList ...!' + err.message);
            res.json(messageUtil.composeFailedMessage(req, err.message));
        } else {
            res.json(messageUtil.composeSucceededMessage(req, null, result));
        }
    })
}

exports.lotteryUser = function (req, res) {
    var lotteryId = req.params.lotteryId;
    var weekId = req.params.weekId;
    var rank = req.params.rank;

    lotteryService.lotteryUserService(lotteryId, weekId, rank, function (err, result) {
        if (err) {
            logger.error('Exception happened when lotteryUser ...!' + err.message);
            res.json(messageUtil.composeFailedMessage(req, err.message));
        } else {
            res.json(messageUtil.composeSucceededMessage(req, null, result));
        }
    })
};

exports.getLuckyUser = function (req, res) {
    var lotteryId = req.params.lotteryId;
    var weekId = req.params.weekId;

    lotteryService.getLuckyUserService(lotteryId, weekId, function (err, result) {
        if (err) {
            logger.error('Exception happened when getLuckyUser ...!' + err.message);
            res.json(messageUtil.composeFailedMessage(req, err.message));
        } else {
            res.json(messageUtil.composeSucceededMessage(req, null, result));
        }
    })
}

exports.getLotteryActivity = function (req, res) {
    var communityId = req.params.communityId;
    lotteryService.getLotteryActivityService(communityId, function (err, result) {
        if (err) {
            logger.error('Exception happened when getLotteryActivity ...!' + err.message);
            res.json(messageUtil.composeFailedMessage(req, err.message));
        } else {
            res.json(messageUtil.composeSucceededMessage(req, null, result));
        }
    })
};

/**
 * 开奖
 * @param req
 * @param res
 */
exports.openLottery = function (req, res) {
    var lotteryId = req.params.lotteryId;
    var weekId = req.params.weekId;
    lotteryService.openLotteryService(lotteryId, weekId, function (err, result) {
        if (err) {
            logger.error('Exception happened when openLottery ...!' + err.message);
            res.json(messageUtil.composeFailedMessage(req, err.message));
        } else {
            res.json(messageUtil.composeSucceededMessage(req, null, result));
        }
    })
};


exports.getUserInfo = function (req, res) {
    var token = req.params.token;
    var activityId = req.params.activityId;

    lotteryService.getUserInfoService(token, activityId, function (err, result) {
        if (err) {
            logger.error('Exception happened when getUserInfo ...!' + err.message);
            res.json(messageUtil.composeFailedMessage(req, 'Exception happened when getUserInfo ...!' + err.message));
        } else {
            res.json(messageUtil.composeSucceededMessage(req, null, result));
        }
    })
};

exports.doRegisterActivity = function (req, res) {
    var token = req.params.token;
    var activityId = req.params.activityId;
    var input = req.body;

    lotteryService.doRegisterActivityService(token, activityId, input, function (err, result) {
        if (err) {
            logger.error('Exception happened when doRegisterActivity ...!' + err.message);
            res.json(messageUtil.composeFailedMessage(req, 'Exception happened when doRegisterActivity ...!' + err.message));
        } else {
            res.json(messageUtil.composeSucceededMessage(req, null, null));
        }
    })
};
